Option Strict On
Option Explicit On 

Public Class Title
    Inherits System.Windows.Forms.Control

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

        setstyle(ControlStyles.ResizeRedraw, True)
    End Sub

    'UserControl overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents ImageList1 As System.Windows.Forms.ImageList
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Title))
        Me.ImageList1 = New System.Windows.Forms.ImageList(Me.components)
        '
        'ImageList1
        '
        Me.ImageList1.ImageSize = New System.Drawing.Size(7, 8)
        Me.ImageList1.ImageStream = CType(resources.GetObject("ImageList1.ImageStream"), System.Windows.Forms.ImageListStreamer)
        Me.ImageList1.TransparentColor = System.Drawing.Color.Silver

    End Sub

#End Region

    Private boxRect As Rectangle
    Private inBox As Boolean
    Private onBox As Boolean

    Private mSelected As Boolean
    Private Const HEADER_HEIGHT As Integer = 16

    Public Event ButtonClick As EventHandler

    Protected Sub OnButtonClick()
        RaiseEvent ButtonClick(Me, New EventArgs)
    End Sub

    Protected Overrides Sub OnMouseLeave(ByVal e As System.EventArgs)
        MyBase.OnMouseLeave(e)

        If onBox Then
            onBox = False
            Invalidate(boxRect)
        End If
    End Sub
    Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseDown(e)

        inBox = boxRect.Contains(e.X, e.Y)
        If inBox Then Invalidate(boxRect)
    End Sub
    Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseMove(e)

        Dim h As Boolean = boxRect.Contains(e.X, e.Y)
        If onBox <> h Then
            onBox = h
            Invalidate(boxRect)
        End If
    End Sub
    Protected Overrides Sub OnMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseUp(e)

        If inBox Then
            inBox = False
            Invalidate(boxRect)
            If onBox Then OnButtonClick()
        End If
    End Sub
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)

        Try
            Dim imageIndex As Byte
            Dim rectF As New RectangleF(1, 1, Me.Width - ImageList1.ImageSize.Width - 10, HEADER_HEIGHT - 2)
            If mSelected Then
                e.Graphics.FillRectangle(SystemBrushes.Highlight, New Rectangle(0, 0, Me.Width, HEADER_HEIGHT))
                e.Graphics.DrawString(Me.Text, Me.Font, SystemBrushes.HighlightText, rectF)
                imageIndex = 0
            Else
                e.Graphics.DrawRectangle(SystemPens.ControlDark, New Rectangle(0, 0, Me.Width - 1, HEADER_HEIGHT - 1))
                e.Graphics.DrawString(Me.Text, Me.Font, SystemBrushes.ControlText, rectF)
                imageIndex = 1
            End If

            Dim p As Point = boxRect.Location
            p.Offset(3, 3)
            If inBox Then
                If onBox Then
                    ControlPaint.DrawBorder3D(e.Graphics, boxRect, Border3DStyle.SunkenInner)
                    p.Offset(1, 1)
                Else
                    ControlPaint.DrawBorder3D(e.Graphics, boxRect, Border3DStyle.RaisedInner)
                End If
                ImageList1.Draw(e.Graphics, p, imageIndex)
            Else
                If onBox Then
                    ControlPaint.DrawBorder3D(e.Graphics, boxRect, Border3DStyle.RaisedInner)
                End If
                ImageList1.Draw(e.Graphics, p, imageIndex)
            End If
        Catch ex As Exception
            Trace.WriteLine(ex.Message, "Title/OnPaint")
        End Try
    End Sub
    Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
        MyBase.OnResize(e)

        boxRect = New Rectangle(Me.Width - ImageList1.ImageSize.Width - 8, 1, 14, 14)
    End Sub
    Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
        MyBase.OnTextChanged(e)

        Invalidate()
    End Sub

    Public Property Selected() As Boolean
        Get
            Return mSelected
        End Get
        Set(ByVal Value As Boolean)
            mSelected = Value
            Invalidate()
        End Set
    End Property
End Class
